import { Dir } from "../type/dir";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    speed: number = 300;

    moveDir: cc.Vec2 = cc.v2(1,0);
    dir: Dir = Dir.up

    onLoad() {
        this.node.on('setDir', this.setDir, this);
    }

    update(dt: number) {
        this.node.x += this.speed * dt * this.moveDir.x;
        this.node.y += this.speed * dt * this.moveDir.y;
    }

    setDir(dir: Dir) {
        this.dir = dir;
        switch (dir) {
            case Dir.up:
                this.moveDir = cc.v2(0, 1)
                break;
            case Dir.right:
                this.moveDir = cc.v2(1, 0)
                break;
            case Dir.down:
                this.moveDir = cc.v2(0, -1)
                break;
            case Dir.left:
                this.moveDir = cc.v2(-1, 0)
                break;
             
        }
    }
}
